// pages/portal/sign/confirm/confirm.js
var app = getApp()

// 引入SDK核心类
var QQMapWX = require('../../../../utils/qqmap-wx-jssdk.min.js');

// 实例化API核心类
var qqmapsdk = new QQMapWX({
  key: 'Q7SBZ-B7JWR-TKNW7-WDKNJ-5GUVO-YKFP5' // 必填
});


Page({

  /**
   * 页面的初始数据
   */
  data: {
    signInfo: {},
    submitDisbale: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    //获取扫码传输过来的参数
    var scene = options.scene
    console.log(scene)
    this.setData({
      scene: scene
    })
    this.getSignInfo()
    this.getLocaltion()
  },

  /** 提交签到 */
  formSubmit: function (res) {
    var that = this
    console.log(res)
    this.setData({
      submitDisbale: 1
    })
    //获取提交的formId
    var formId = res.detail.formId
    //判断距离是否合法
    var distance = this.data.distance
    if(distance>3000){
      wx.showToast({
        title: '不在签到范围内!',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.showLoading({
      title: '加载中',
    })
    //提交签到
    wx.request({
      url: app.host + '/sign/to',
      header: {
        accessToken: app.accessToken
      },
      data: {
        params: that.data.scene,
        longitude: that.data.userLatitude,
        latitude: that.data.userLongitude
      },
      method: 'get',
      complete: function (e) {
        console.log(e)
        wx.hideLoading()
        //取消按钮禁用
        that.setData({
          submitDisbale: 0
        })
        if (e.statusCode == 200) {
          if (e.data.code == 0) {
            wx.showToast({
              title: '签到成功！',
              duration: 2000
            })
          } else {
            wx.showToast({
              title: e.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      }
    })
  },

  /** 获取本次签到的基本信息 */
  getSignInfo: function (res) {
    var that = this
    //请求签到信息
    wx.request({
      url: app.host + '/sign/qrcode/info',
      header: {
        accessToken: app.accessToken
      },
      data: {
        params: that.data.scene
      },
      method: 'get',
      complete: function (e) {
        console.log(e)
        if (e.statusCode == 200) {
          if (e.data.code == 0) {
            var resData = e.data.data
            var lat = parseFloat(resData.latitude)
            var lon = parseFloat(resData.longitude)
            that.setData({
              signInfo: resData,
              signLatitude: lat,
              signLongitude: lon
            })
            that.calculateDistance()
          }
        }
      }
    })
  },

  /** 获取地理位置信息 */
  getLocaltion: function (res) {
    var that = this
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        console.log(res)
        that.setData({
          userLatitude: res.latitude,
          userLongitude: res.longitude
        })
      }
    })
  },

  /** 计算距离 */
  calculateDistance: function (e) {
    var _this = this;
    console.log(_this.data.signLatitude)
    //调用距离计算接口
    qqmapsdk.calculateDistance({
      //from参数不填默认当前地址
      to: [{
        latitude: _this.data.signLatitude,
        longitude: _this.data.signLongitude
      }],
      success: function (res) { //成功后的回调
        console.log(res);
        var res = res.result;
        var dis = [];
        for (var i = 0; i < res.elements.length; i++) {
          dis.push(res.elements[i].distance); //将返回数据存入dis数组，
        }
        _this.setData({ //设置并更新distance数据
          distance: dis
        });
      },
      fail: function (error) {
        console.error(error);
      }
    });
  },

  scanCode: function (res) {
    console.log("打开扫一扫")
    // 只允许从相机扫码
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        console.log(res)
        var path = res.path
        console.log(path)
        wx.redirectTo({
          url: "/" + path,
        })
      }
    })
  }


})